package com.anzuo.app.util

import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

/**
 * 扩展函数文件
 * 提供常用的扩展函数，简化代码
 * 
 * @author AnZuo
 * @date 2024
 */

/**
 * Fragment扩展函数：显示Toast
 */
fun Fragment.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(requireContext(), message, duration).show()
}

/**
 * Fragment扩展函数：显示长Toast
 */
fun Fragment.showLongToast(message: String) {
    showToast(message, Toast.LENGTH_LONG)
}

/**
 * LiveData扩展函数：简化观察LiveData的代码
 */
fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) {
    observe(lifecycleOwner, object : Observer<T> {
        override fun onChanged(value: T) {
            observer.onChanged(value)
            removeObserver(this)
        }
    })
}

